package org.yscz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.yscz.entity.Option;
import org.yscz.entity.Question;
import org.yscz.entity.Scale;
import org.yscz.entity.Score;
import org.yscz.entity.vo.QuestionVo;
import org.yscz.entity.vo.ScaleVo;
import org.yscz.mapper.OptionMapper;
import org.yscz.mapper.QuestionMapper;
import org.yscz.mapper.ScaleMapper;
import org.yscz.mapper.ScoreMapper;
import org.yscz.service.ScaleService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author muqs
 * @create 2021-08-19 17:48
 */
@Service
@Slf4j
public class ScaleServiceImpl extends ServiceImpl<ScaleMapper, Scale> implements ScaleService {

    @Autowired
    private ScaleMapper scaleMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private OptionMapper optionMapper;

    @Autowired
    private OptionServiceImpl optionServic;

    @Autowired
    private QuestionServiceImpl questionService;

    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public Scale add(ScaleVo scaleVo) {
        Scale scale = new Scale();
        if (scaleVo != null) {
            BeanUtils.copyProperties(scaleVo, scale);
            scale.setUpdateTime(new Date());
            scaleMapper.insert(scale);

            List<Score> scores = scaleVo.getScoreList();

            if (!CollectionUtils.isEmpty(scores)) {
                scores.stream().forEach(item -> {
                    item.setScaleIdFk(scale.getScaleId());
                    scoreMapper.insert(item);
                });
            }
        }

        return scale;
    }

    @Override
    public void modify(ScaleVo scaleVo) {

        Scale scale = new Scale();
        if (scaleVo != null) {
            BeanUtils.copyProperties(scaleVo, scale);
            scale.setUpdateTime(new Date());
            scaleMapper.updateById(scale);

            // 先将量表分数数据删除，然后重新插入
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("scale_id_fk", scaleVo.getScaleId());
            scoreMapper.delete(queryWrapper);

            List<Score> scores = scaleVo.getScoreList();
            if (!CollectionUtils.isEmpty(scores)) {
                scores.stream().forEach(item -> {
                    item.setScaleIdFk(scaleVo.getScaleId());
                    scoreMapper.insert(item);
                });
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Long scaleId) {
        scaleMapper.deleteById(scaleId);

        // 查询量表对应题目数据
        QueryWrapper questionWrapper = new QueryWrapper();
        questionWrapper.eq("scale_id_fk", scaleId);
        List<Question> questions = questionMapper.selectList(questionWrapper);

        if (!CollectionUtils.isEmpty(questions)) {
            List<Long> questionIds = new ArrayList<>();
            questions.stream().forEach(item -> {
                questionIds.add(item.getQuestionId());
            });

            // 删除题目数据
            questionMapper.deleteBatchIds(questionIds);
            // 删除题目选项数据
            QueryWrapper optionWrapper = new QueryWrapper();
            optionWrapper.in("question_id_fk", questionIds);
            optionMapper.delete(optionWrapper);

            // 删除分数数据
            QueryWrapper scoreWrapper = new QueryWrapper();
            scoreWrapper.in("scale_id_fk", scaleId);
            scoreMapper.delete(scoreWrapper);
        }
    }

    @Override
    public IPage<Scale> listPage(QueryWrapper queryWrapper, IPage<Scale> page) {
        return scaleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<ScaleVo> listPageByCondition(IPage<Scale> page, Map condition) {
        return scaleMapper.listPageByCondition(page, condition);
    }

    @Override
    public void questionAdd(List<QuestionVo> questionVoList) {
        List<Option> options = new ArrayList<>();
        questionVoList.stream().forEach(item -> {
            // 题目信息入库
            Question question = new QuestionVo();
            BeanUtils.copyProperties(item, question);
            questionMapper.insert(question);

            // 处理题目选项信息
            if (!CollectionUtils.isEmpty(item.getOptionList())) {
                item.getOptionList().stream().forEach(option -> {
                    option.setQuestionIdFk(question.getQuestionId());
                    options.add(option);
                });

            }
        });

        // 选项信息入库
        if (!CollectionUtils.isEmpty(options)) {
            optionServic.saveBatch(options);
        }
    }

    @Override
    public void questionModify(List<QuestionVo> questionVoList) {
        List<Option> options = new ArrayList<>();
        List<Question> questions = new ArrayList<>();
        questionVoList.stream().forEach(item -> {
            // 更新试题信息
            Question question = new QuestionVo();
            BeanUtils.copyProperties(item, question);
            questions.add(question);

            // 处理试题选项信息
            if (!CollectionUtils.isEmpty(item.getOptionList())) {
                item.getOptionList().stream().forEach(option -> {
                    options.add(option);
                });
            }
        });

        // 更新题目信息
        if (!CollectionUtils.isEmpty(questions)) {
            questionService.saveOrUpdateBatch(questions);
        }

        // 更新选项信息
        if (!CollectionUtils.isEmpty(options)) {
            optionServic.saveOrUpdateBatch(options);
        }
    }

    @Override
    public List<QuestionVo> queryByScaleId(Long scaleId) {
        return questionMapper.queryByScaleId(scaleId);
    }

    @Override
    public ScaleVo queryScaleDetail(Long scaleId) {
        return scaleMapper.queryDetailById(scaleId);
    }
}
